<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区  用户的数据 -->
    <el-card>
      <!-- 搜索区域 -->
      <!-- :gutter="20"下面的输入框 与按钮的间距 -->
      <el-row :gutter="20">
        <el-col :span="9">
          <div>
            <el-input clearable  v-model="queryInfo.query" placeholder="请输入内容">
              <!-- 必须绑定v-model才可以输入 -->
              <el-button slot="append" @click="getGoodsList" icon="el-icon-search" ></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区 -->
      <el-table :data="goodslist" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" width="100%" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" width="100%" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="100%" prop="add_time">
           <!-- 处理时间的函数已经在main.js中全局注册过滤了 -->
            <template slot-scope="scope">
                {{scope.row.add_time | dateFormat}}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="200%">
          <template slot-scope="scope">
            <!-- 修改按钮 @click="showEditDialog(scope.row.id)"-->
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <!-- 删除按钮 "-->
            <el-button type="danger"  icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <!-- 分页功能 size-change改变事件-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total" background
        ></el-pagination><!-- layout分页下面功能的布局 -->
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //页脚数据
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      //商品列表数据
      goodslist: [],
      //总条数
      total: 0,
    };
  },

  components: {},

  computed: {},

  created() {
    this.getGoodsList();
  },

  methods: {
    //根据分页获取对应商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      }); //params:this.queryInfo传一个数组用parmas

      if (res.meta.status !== 200) {
        return this.$message.error("获取数据失败，请刷新页面");
      }

      this.goodslist = res.data.goods;
      this.total = res.data.total;
    },
    //监听pagesize 改变
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize;
      this.getGoodsList()
    },
    //监听 页码值 改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList()
    },
    //删除商品
    async removeById(id){
         const confirmResult = await this.$confirm("即将删除该商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch((err) => err);

      //取消删除商品
      if (confirmResult !== "confirm") {
        return this.$message.info("取消删除");
      }

      //删除
      const { data: res } = await this.$http.delete(
        `goods/${id}`
      );

      if (res.meta.status !== 200) {
        return this.$message.error("删除失败");
      }

      this.$message.success("删除成功！");

      this.getGoodsList()
    },
    //跳转到添加商品页面
    goAddpage(){
        this.$router.push('/goods/add')
    }
  },
};
</script>
<style lang='css' scoped>
.block{
    margin-top: 10px;
}
</style>